#!/usr/bin/python
# -*- coding:utf-8 -*-
# @FileName : Test1.py
# Author    : myh
import torch
import os
import pandas as pd
import numpy as np
from matplotlib_inline import backend_inline

# x = torch.arange(12)
#
# print(x)
#
# print(x.shape)

# 创建文件
# os.makedirs(os.path.join('../data/house_tiny.csv', 'data'),exist_ok=True)
data_file = os.path.join('../data/house_tiny.csv', 'data', 'house_tiny.csv')
# with open(data_file, 'w') as f:
#     f.write('NumRooms,Alley,Price\n')  # 列名
#     f.write('NA,Pave,127500\n')  # 每行表示一个数据样本
#     f.write('2,NA,106000\n')
#     f.write('4,NA,178100\n')
#     f.write('NA,NA,140000\n')

# data = pd.read_csv(data_file)
# print(data)
# print()
# # 处理缺失值
# inputs, outputs = data.iloc[:, 0:2], data.iloc[:, 2]
# print(inputs)
# print()
# print(outputs)
# inputs = inputs.fillna(inputs.mean(numeric_only=True))
# print(inputs)
#
# inputs = pd.get_dummies(inputs, dummy_na=True)
# print(inputs)
#
# X = torch.tensor(inputs.to_numpy(dtype=float))
# y = torch.tensor(outputs.to_numpy(dtype=float))
# print(X)
# print(y)


# 求导
def f(x):
    return 3 * x ** 2 - 4 * x


def numerical_lim(f, x, h):
    return (f(x + h) - f(x)) / h


def use_svg_display():  #@save
    """使用svg格式在Jupyter中显示绘图"""
    backend_inline.set_matplotlib_formats('svg')


def set_figsize(figsize=(3.5, 2.5)):  #@save
    """设置matplotlib的图表大小"""
    use_svg_display()
    np.plt.rcParams['figure.figsize'] = figsize



h = 0.1
for i in range(5):
    print(f'h={h:.5f}, numerical limit={numerical_lim(f, 1, h):.5f}')
    h *= 0.1


